﻿using System.Linq;
using GNSCore.Managers;
using GNSCore.Model;
using GNSCore.Model.Context;
using GNSCore.Repositories;
using GNSCore.Repositories.IOC;

namespace GNSCore.Helpers
{
    /// <summary>
    /// For managin location
    /// </summary>
    public static class LocationInfoHelper
    {
        static IRepository<Location> _locationRepository;

        static IGNSContext _context;

        static LocationInfoHelper()
        {
            _locationRepository = IOCManager.IOCContainer.Resolve<IRepository<Location>>();
            _context = IOCManager.IOCContainer.Resolve<IGNSContext>();
            _locationRepository.SetContext(_context);
        }

        /// <summary>
        /// Returns location for specefied user
        /// </summary>
        /// <param name="username">Username</param>
        /// <returns>User location information</returns>
        public static Location GetLocationForUser(string username)
        {
            Location location = _locationRepository.GetAll<Location>().FirstOrDefault(loc => loc.User.UserName == username);
            return location;
        }
    }
}